import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';

class ExpansionPanelListPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _ExpansionPanelListPage();
  }
}

class _ExpansionPanelListPage extends State<ExpansionPanelListPage> {
  var currentPanelIndex = -1;
  List<int> mList;

  _ExpansionPanelListPage() {
    mList = new List();
    for (int i = 0; i < 10; i++) {
      mList.add(i);
    }
  }

  void _setCurrentIndex(int index) {
    setState(() {
      if (currentPanelIndex == index) {
        index = -1;
      }
      currentPanelIndex = index;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("ExpansionPanelList"),
        centerTitle: true,
      ),
      body: SingleChildScrollView(
        child: ExpansionPanelList(
          children: mList.map((index) {
            return new ExpansionPanel(
                headerBuilder: (context, isExpanded) {
                  return new ListTile(
                    title: new Text('我是第$index个标题'),
                  );
                },
                body: new Padding(
                  padding: EdgeInsets.symmetric(horizontal: 5.0),
                  child: Container(
                    height: 100.0,
                    color: Colors.blue,
                    alignment: Alignment.center,
                    child: Icon(
                      Icons.security,
                      size: 35.0,
                    ),
                  ),
                ),
                isExpanded: currentPanelIndex == index);
          }).toList(),
          expansionCallback: (index, bol) {
            _setCurrentIndex(index);
          },
        ),
      ),
    );
  }
}
